/*
* @author suhong
* @date 2022/5/19 15:48
* @file article.js
* @说明  文章路由的处理函数
*/
//导入数据库模块
const exec=require('../../db/mysql_connect')
const imgIcon=require('../../db/headImg')
exports.userArticle=((req,res)=>{
   // res.send(req.query)
   //获取用户的id
   let userId=req.query.id
   // console.log(req)
   //sql语句
   let sqlStr=`select article_id,article_content,article_title,putout_time,article_img from blog_article where user_id=${userId}`
   exec(sqlStr).then(result=>{
      res.send(result)
   }).catch(reason => {
      res.status(500).send({
         message:reason
      })
   })
})
//上传图片和文章内容
exports.PutoutArticle=((req,res)=>{
   console.log(req)
   // res.send('success')
   let imgUrl=imgIcon.headImg(req,res) //图片路径
   let sqlStr=`insert into blog_article (user_id,article_title,article_content,article_sort,article_img) values (${req.body.id},'${req.body.title}','${req.body.text}',${req.body.type},'${imgUrl}')`
   exec(sqlStr).then(()=>{
      res.send({
         status:0,
         message:'success'
      })
   }).catch(reason => {
      res.send({
         status:1,
         message:reason
      })
   })
   
})
//获取某个文章详情
exports.getArticle=((req,res)=>{
   //获取文章id
   let ArticleId=req.query.id
   //sql语句
   let sqlStr=`select putout_time,article_title,article_content,article_img from blog_article where article_id=${ArticleId}`
   exec(sqlStr).then((value)=>{
      res.send(value)
   }).catch(reason => {
      res.send(reason)
   })
})
//删除文章
exports.delArticle=((req,res)=>{
   //文章id
   let articleId=req.query.articleId
   //sql语句
   let sqlStr=`delete from blog_article where article_id=${articleId}`
   exec(sqlStr).then(()=>{
      res.send({
         status:0,
         message:'success'
      })
   }).catch(reason => {
      res.send(reason)
   })
})
//用户端首页展示信息
exports.ShowHome=(async (req,res)=>{
   //一共多少条
   let sumpage=await exec('select * from blog_article')
   sumpage=sumpage.length
   //多少页
   let page=req.query.page || 1
   let sqlStr=`select blog_user.user_name,blog_user.user_icon,blog_article.article_id,blog_article.article_img,blog_article.putout_time,blog_article.article_title,blog_article.article_content,blog_article.article_sort from blog_user,blog_article where blog_article.user_id=blog_user.user_id ORDER BY blog_article.putout_time desc limit ${(page-1)*5},5`
   exec(sqlStr).then(value => {
      res.send({
         sumpage:sumpage,
         data:value
      })
   }).catch(reason => {
      res.send(reason)
   })
})